﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Map extends MovieClip {
		private var enemies:Vector.<enemy>;
		private var obstacles:Vector.<obstacle>;
		public var pb:playerBlast;
		
		public function Map() {
			addEventListener(Event.ENTER_FRAME, update);
			enemies = new Vector.<enemy>();
			enemies.push(e1);
			enemies.push(e2);
			enemies.push(e3);
			enemies.push(e4);
			enemies.push(e5);
			enemies.push(e6);
			enemies.push(e7);
			enemies.push(e8);
			enemies.push(e9);
			
			obstacles = new Vector.<obstacle>();
			obstacles.push(o1);
			obstacles.push(o2);
			obstacles.push(o3);
			obstacles.push(o4);
			obstacles.push(o5);
			obstacles.push(o6);
			obstacles.push(o7);
			obstacles.push(o8);
			obstacles.push(o9);
			obstacles.push(o0);
			
			dude.obst = obstacles;
		}
		
		public function update(e:Event):void
		{
			
			moveMap();
			checkCollisions();
			
		}
		
		public function checkCollisions():void
		{
			if(pb != null)
			{
				for(var p:int = 0; p < enemies.length; i++)
				{
				
					if(enemies[p] != null)
					{
						
						if(enemies[p].hitTestObject(pb))
						{
							pb.BOOM();
							break;
						}
					}
				}
			
				for(var o:int = 0; o < enemies.length; i++)
				{
					if(obstacles[o].hitTestObject(pb))
					{
						pb.BOOM();
						break;
					}
				}
			}
			
			for(var i:int = 0; i < enemies.length; i++)
			{
				if(enemies[i] != null)
				{
					if(enemies[i].hitTestObject(dude.meleeArm.hit) && dude.meleeArm.currentFrameLabel != "Idle")
					{
						removeChild(enemies[i]);
					}
					
					if(pb != null)
					{
						if(enemies[i].hitTestObject(pb))
						{
							removeChild(enemies[i]);
						}
					}
				}
			}
		}
		
		public function moveMap():void
		{
			x = -dude.x + 427; y = -dude.y + 240;
			
			if(x < -1146)
			{ 
				x = -1146;
			}
			else if (x > 0)
			{
				x = 0;
			}
			if(y < -520)
			{
				y = -520;
			}
			else if(y > 0)
			{
				y = 0;
			}
		}
	}
	
}
